
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.util {
import org.waveprotocol.wave.model.id.PercentEncoderDecoder;

/**
 * Uses standard java URLEncoder and URLDecoder to percent encode.
 */
public class URLEncoderDecoderBasedPercentEncoderDecoder
	implements PercentEncoderDecoder {
	
	public function decode(encodedValue:String):String {
		try {
			//TODO unit test this...
			var ret:String= escape(encodedValue);
			if (ret.indexOf("0xFFFD") != -1) {
				throw new Error(
					"Unable to decode value " + encodedValue + " it contains invalid UTF-8");
			}
			return ret;
		} catch (e:Error) {
			throw new Error("Encoding Exception: " +e);
		}
		return encodedValue;
	}
	
	
	public function encode(decodedValue:String):String   {
		try {
			//TODO unit test this...
			return unescape(decodedValue);
		} catch (e) {
			throw new Error("Encoding Exception: " +e);
		}
		return decodedValue;
	}
}
}